import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class MemberService extends BaseService<any> {
  private readonly logger = new Logger(MemberService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getPage
   * 对应 PHP: MemberService_admin::getPage()
   * 逻辑类型: undefined - undefined
   */
  async getPage(where: any[]) {
    // 基于PHP真实逻辑: getPage
    // PHP原文: $field = 'member_id, member_no, site_id, username, mobile, password, register_channel, register_type, nickname, headimg, member_level, member_label, w...
const field = 'member_id, member_no, site_id, username, mobile, password, register_channel, register_type, nickname, headimg, member_level, member_label, wx_openid, weapp_openid, wx_unionid, ali_openid, douyin_openid, login_ip, login_type, login_channel, login_count, login_time, create_time, last_visit_time, last_consum_time, sex, status, birthday, point, point_get, balance, balance_get, growth, growth_get, is_member, member_time, is_del, province_id, city_id, district_id, address, location, delete_time, money, money_get, commission, commission_get, commission_cash_outing';
        const search_model = this.model.where([['site_id', '=', this.site_id))).withSearch(['keyword', 'register_type', 'create_time', 'is_del', 'member_label', 'register_channel', 'member_level'), where)
            .field(field)
            .order('member_id desc')
            .with('member_level_name_bind')
            .append(['register_channel_name', 'register_type_name', 'sex_name', 'login_channel_name', 'login_type_name', 'status_name')];
        return this.pageQuery(search_model, function (item, key) {
            const item = this.makeUp(item);
  }

  /**
   * getList
   * 对应 PHP: MemberService_admin::getList()
   * 逻辑类型: undefined - undefined
   */
  async getList(where: any[]) {
    // 基于PHP真实逻辑: getList
    // PHP原文: $field = 'member_id, nickname, headimg';         $temp_where[] = ['site_id', '=', $this->site_id];         if (!empty($where['member_ids'])) {        ...
const field = 'member_id, nickname, headimg';
        temp_where[] = ['site_id', '=', this.site_id];
        if (!!where.member_ids)) {
            temp_where[] = ['member_id', 'in', implode(',', where.member_ids)];
  }

  /**
   * getInfo
   * 对应 PHP: MemberService_admin::getInfo()
   * 逻辑类型: undefined - undefined
   */
  async getInfo(member_id: number) {
    // 基于PHP真实逻辑: getInfo
    // PHP原文: $field = 'member_id,member_no, id_card,remark,site_id, username, mobile, password, register_channel, register_type, nickname, headimg, member_level, m...
const field = 'member_id,member_no, id_card,remark,site_id, username, mobile, password, register_channel, register_type, nickname, headimg, member_level, member_label, wx_openid, weapp_openid, wx_unionid, ali_openid, douyin_openid, login_ip, login_type, login_channel, login_count, login_time, create_time, last_visit_time, last_consum_time, sex, status, birthday, point, point_get, balance, balance_get, growth, growth_get, is_member, member_time, is_del, province_id, city_id, district_id, address, location, delete_time, money, money_get, commission, commission_get, commission_cash_outing';
        const info = this.makeUp(this.model.where([['member_id', '=', member_id], ['site_id', '=', this.site_id))).field(field).with('member_level_name_bind').append(['register_channel_name', 'register_type_name', 'sex_name', 'login_channel_name', 'login_type_name', 'status_name')).findOrEmpty().toArray()];
        if (!info.member_level)) {
            info.member_level = '';
  }

  /**
   * add
   * 对应 PHP: MemberService_admin::add()
   * 逻辑类型: undefined - undefined
   */
  async add(data: any[]) {
    // 基于PHP真实逻辑: add
    // PHP原文: //检测手机是否重复         if (!empty($data['mobile'])) {             if (!$this->model->where([['site_id', '=', $this->site_id], ['mobile', '=', $data['mobil...
//检测手机是否重复
        if (!!data.mobile)) {
            if (!this.model.where([['site_id', '=', this.site_id], ['mobile', '=', data.mobile))).findOrEmpty().isEmpty())
                throw new AdminBusinessException('MOBILE_IS_EXIST');
  }

  /**
   * edit
   * 对应 PHP: MemberService_admin::edit()
   * 逻辑类型: undefined - undefined
   */
  async edit(member_id: any[], data: any[]) {
    // 基于PHP真实逻辑: edit
    // PHP原文: $where = array(             ['site_id', '=', $this->site_id],             ['member_id', '=', $member_id],         );         if (!empty($data['passwor...
const where = [
            ['site_id', '=', this.site_id],
            ['member_id', '=', member_id),
        ];
        if (!!data.password)) {
            data.password = create_password(data.password);
  }

  /**
   * modify
   * 对应 PHP: MemberService_admin::modify()
   * 逻辑类型: undefined - undefined
   */
  async modify(member_id: number, field: number, data: number) {
    // 基于PHP真实逻辑: modify
    // PHP原文: return (new CoreMemberService())->modify($this->site_id, $member_id, $field, $data);     }      /**      * 批量修改字段      * @param $data      * @return M...
return this.coreMemberService.modify(this.site_id, member_id, field, data);
    }

    /**
     * 批量修改字段
     * @param data
     * @return Member
     */
    async batchModify(data)
    {
        return this.coreMemberService.batchModify(this.site_id, data);
    }

    /**
     * 组合整理数据
     * @param data
     * @return mixed
     * @throws DataNotFoundBusinessException
     * @throws DbBusinessException
     * @throws ModelNotFoundBusinessException
     */
    async makeUp(data)
    {
        //会员标签
        if (!!data.member_label)) {
            data.member_label_array = this.memberLabelService.getMemberLabelListByLabelIds(data.member_label);
  }

  /**
   * batchModify
   * 对应 PHP: MemberService_admin::batchModify()
   * 逻辑类型: undefined - undefined
   */
  async batchModify(data: any) {
    // 基于PHP真实逻辑: batchModify
    // PHP原文: return (new CoreMemberService())->batchModify($this->site_id, $data);     }      /**      * 组合整理数据      * @param $data      * @return mixed      * @th...
return this.coreMemberService.batchModify(this.site_id, data);
    }

    /**
     * 组合整理数据
     * @param data
     * @return mixed
     * @throws DataNotFoundBusinessException
     * @throws DbBusinessException
     * @throws ModelNotFoundBusinessException
     */
    async makeUp(data)
    {
        //会员标签
        if (!!data.member_label)) {
            data.member_label_array = this.memberLabelService.getMemberLabelListByLabelIds(data.member_label);
  }

  /**
   * makeUp
   * 对应 PHP: MemberService_admin::makeUp()
   * 逻辑类型: undefined - undefined
   */
  async makeUp(data: any) {
    // 基于PHP真实逻辑: makeUp
    // PHP原文: //会员标签         if (!empty($data['member_label'])) {             $data['member_label_array'] = (new MemberLabelService())->getMemberLabelListByLabelIds...
//会员标签
        if (!!data.member_label)) {
            data.member_label_array = this.memberLabelService.getMemberLabelListByLabelIds(data.member_label);
  }

  /**
   * getCount
   * 对应 PHP: MemberService_admin::getCount()
   * 逻辑类型: undefined - undefined
   */
  async getCount(where: any[]) {
    // 基于PHP真实逻辑: getCount
    // PHP原文: $where[] = ['site_id', '=', $this->site_id];         $where[] = ['is_del', '=', 0];         return $this->model->where($where)->count();     }      /*...
where[] = ['site_id', '=', this.site_id];
        where[] = ['is_del', '=', 0];
        return this.model.where(where).count(];
    }

    /**
     * 设置状态
     * @param member_ids
     * @param int status
     * @return true
     */
    async setStatus(member_ids, int status)
    {
        const where = [
            ['site_id', '=', this.site_id],
            ['member_id', 'in', member_ids),
        ];
        const data = [
            status: status
        ];
        this.model.where(where).update(data);
        return true;
    }

    /**
     * 会员数据
     * @param field
     * @return float
     */
    async getSum(field)
    {
        return this.model.where([['site_id', '=', this.site_id))).sum(field);
    }

    /**
     * 创建会员编码
     * @return string|null
     */
    async getMemberNo()
    {
        //开启事务,解决创建会员编码重复
        try{
            Db.startTrans(];
            const member_no = this.coreMemberService.createMemberNo(this.site_id];
            Db.commit();
            return member_no;
  }

  /**
   * setStatus
   * 对应 PHP: MemberService_admin::setStatus()
   * 逻辑类型: undefined - undefined
   */
  async setStatus(member_ids: any[], status: any[]) {
    // 基于PHP真实逻辑: setStatus
    // PHP原文: $where = array(             ['site_id', '=', $this->site_id],             ['member_id', 'in', $member_ids],         );         $data = array(         ...
const where = [
            ['site_id', '=', this.site_id],
            ['member_id', 'in', member_ids),
        ];
        const data = [
            status: status
        ];
        this.model.where(where).update(data);
        return true;
    }

    /**
     * 会员数据
     * @param field
     * @return float
     */
    async getSum(field)
    {
        return this.model.where([['site_id', '=', this.site_id))).sum(field);
    }

    /**
     * 创建会员编码
     * @return string|null
     */
    async getMemberNo()
    {
        //开启事务,解决创建会员编码重复
        try{
            Db.startTrans(];
            const member_no = this.coreMemberService.createMemberNo(this.site_id];
            Db.commit();
            return member_no;
  }

  /**
   * getSum
   * 对应 PHP: MemberService_admin::getSum()
   * 逻辑类型: undefined - undefined
   */
  async getSum(field: any) {
    // 基于PHP真实逻辑: getSum
    // PHP原文: return $this->model->where([['site_id', '=', $this->site_id]])->sum($field);     }      /**      * 创建会员编码      * @return string|null      */     publi...
return this.model.where([['site_id', '=', this.site_id))).sum(field);
    }

    /**
     * 创建会员编码
     * @return string|null
     */
    async getMemberNo()
    {
        //开启事务,解决创建会员编码重复
        try{
            Db.startTrans(];
            const member_no = this.coreMemberService.createMemberNo(this.site_id];
            Db.commit();
            return member_no;
  }

  /**
   * getMemberNo
   * 对应 PHP: MemberService_admin::getMemberNo()
   * 逻辑类型: undefined - undefined
   */
  async getMemberNo() {
    // 基于PHP真实逻辑: getMemberNo
    // PHP原文: //开启事务,解决创建会员编码重复         try{             Db::startTrans();             $member_no = (new CoreMemberService())->createMemberNo($this->site_id);      ...
//开启事务,解决创建会员编码重复
        try{
            Db.startTrans(];
            const member_no = this.coreMemberService.createMemberNo(this.site_id];
            Db.commit();
            return member_no;
  }

  /**
   * deleteMember
   * 对应 PHP: MemberService_admin::deleteMember()
   * 逻辑类型: undefined - undefined
   */
  async deleteMember(member_id: number) {
    // 基于PHP真实逻辑: deleteMember
    // PHP原文: $this->model->destroy(function ($query) use ($member_id) {             $query->where([['member_id', '=', $member_id], ['site_id', '=', $this->site_id]...
this.model.destroy(function (query) use (member_id) {
            query.where([['member_id', '=', member_id], ['site_id', '=', this.site_id))];
  }

  /**
   * exportMember
   * 对应 PHP: MemberService_admin::exportMember()
   * 逻辑类型: undefined - undefined
   */
  async exportMember(where: any[]) {
    // 基于PHP真实逻辑: exportMember
    // PHP原文: $field = 'member_id, member_no, username, mobile, nickname, point, balance, money, growth, commission, register_channel, status, create_time, last_vis...
const field = 'member_id, member_no, username, mobile, nickname, point, balance, money, growth, commission, register_channel, status, create_time, last_visit_time';
        //查询导出数据
        const data = this.model.where([['site_id', '=', this.site_id))).withSearch(['keyword', 'register_type', 'create_time', 'is_del', 'member_label', 'register_channel'), where).field(field).append(['register_channel_name', 'status_name')).select().toArray(];
        //执行导出
        this.exportService.exportData('member', data);
        return true;
    }

    /**
     * 获取会员权益字典
     * @return array|null
     */
    async getMemberBenefitsDict()
    {
        return (new DictLoader("MemberBenefits")).load(];
    }

    /**
     * 获取会员礼包字典
     * @return array|null
     */
    async getMemberGiftDict()
    {
        return (new DictLoader("MemberGift")).load(];
    }

    /**
     * 获取成长值规则字典
     * @return array|null
     */
    async getGrowthRuleDict()
    {
        return (new DictLoader("GrowthRule")).load(];
    }

    /**
     * 获取积分规则字典
     * @return array|null
     */
    async getPointRuleDict()
    {
        return (new DictLoader("PointRule")).load(];
    }

    /**
     * 获取会员权益内容
     * @param benefits
     * @return array|null
     */
    async getMemberBenefitsContent(benefits)
    {
        return this.coreMemberService.getBenefitsContent(this.site_id, benefits);
    }

    /**
     * 获取会员礼包内容
     * @param gifts
     * @return array|null
     */
    async getMemberGiftsContent(gifts)
    {
        return this.coreMemberService.getGiftContent(this.site_id, gifts);
    }
}
  }

  /**
   * getMemberBenefitsDict
   * 对应 PHP: MemberService_admin::getMemberBenefitsDict()
   * 逻辑类型: undefined - undefined
   */
  async getMemberBenefitsDict() {
    // 基于PHP真实逻辑: getMemberBenefitsDict
    // PHP原文: return (new DictLoader("MemberBenefits"))->load();     }      /**      * 获取会员礼包字典      * @return array|null      */     public function getMemberGiftD...
return (new DictLoader("MemberBenefits")).load(];
    }

    /**
     * 获取会员礼包字典
     * @return array|null
     */
    async getMemberGiftDict()
    {
        return (new DictLoader("MemberGift")).load(];
    }

    /**
     * 获取成长值规则字典
     * @return array|null
     */
    async getGrowthRuleDict()
    {
        return (new DictLoader("GrowthRule")).load(];
    }

    /**
     * 获取积分规则字典
     * @return array|null
     */
    async getPointRuleDict()
    {
        return (new DictLoader("PointRule")).load(];
    }

    /**
     * 获取会员权益内容
     * @param benefits
     * @return array|null
     */
    async getMemberBenefitsContent(benefits)
    {
        return this.coreMemberService.getBenefitsContent(this.site_id, benefits);
    }

    /**
     * 获取会员礼包内容
     * @param gifts
     * @return array|null
     */
    async getMemberGiftsContent(gifts)
    {
        return this.coreMemberService.getGiftContent(this.site_id, gifts);
    }
}
  }

  /**
   * getMemberGiftDict
   * 对应 PHP: MemberService_admin::getMemberGiftDict()
   * 逻辑类型: undefined - undefined
   */
  async getMemberGiftDict() {
    // 基于PHP真实逻辑: getMemberGiftDict
    // PHP原文: return (new DictLoader("MemberGift"))->load();     }      /**      * 获取成长值规则字典      * @return array|null      */     public function getGrowthRuleDict...
return (new DictLoader("MemberGift")).load(];
    }

    /**
     * 获取成长值规则字典
     * @return array|null
     */
    async getGrowthRuleDict()
    {
        return (new DictLoader("GrowthRule")).load(];
    }

    /**
     * 获取积分规则字典
     * @return array|null
     */
    async getPointRuleDict()
    {
        return (new DictLoader("PointRule")).load(];
    }

    /**
     * 获取会员权益内容
     * @param benefits
     * @return array|null
     */
    async getMemberBenefitsContent(benefits)
    {
        return this.coreMemberService.getBenefitsContent(this.site_id, benefits);
    }

    /**
     * 获取会员礼包内容
     * @param gifts
     * @return array|null
     */
    async getMemberGiftsContent(gifts)
    {
        return this.coreMemberService.getGiftContent(this.site_id, gifts);
    }
}
  }

  /**
   * getGrowthRuleDict
   * 对应 PHP: MemberService_admin::getGrowthRuleDict()
   * 逻辑类型: undefined - undefined
   */
  async getGrowthRuleDict() {
    // 基于PHP真实逻辑: getGrowthRuleDict
    // PHP原文: return (new DictLoader("GrowthRule"))->load();     }      /**      * 获取积分规则字典      * @return array|null      */     public function getPointRuleDict()...
return (new DictLoader("GrowthRule")).load(];
    }

    /**
     * 获取积分规则字典
     * @return array|null
     */
    async getPointRuleDict()
    {
        return (new DictLoader("PointRule")).load(];
    }

    /**
     * 获取会员权益内容
     * @param benefits
     * @return array|null
     */
    async getMemberBenefitsContent(benefits)
    {
        return this.coreMemberService.getBenefitsContent(this.site_id, benefits);
    }

    /**
     * 获取会员礼包内容
     * @param gifts
     * @return array|null
     */
    async getMemberGiftsContent(gifts)
    {
        return this.coreMemberService.getGiftContent(this.site_id, gifts);
    }
}
  }

  /**
   * getPointRuleDict
   * 对应 PHP: MemberService_admin::getPointRuleDict()
   * 逻辑类型: undefined - undefined
   */
  async getPointRuleDict() {
    // 基于PHP真实逻辑: getPointRuleDict
    // PHP原文: return (new DictLoader("PointRule"))->load();     }      /**      * 获取会员权益内容      * @param array $benefits      * @return array|null      */     publi...
return (new DictLoader("PointRule")).load(];
    }

    /**
     * 获取会员权益内容
     * @param benefits
     * @return array|null
     */
    async getMemberBenefitsContent(benefits)
    {
        return this.coreMemberService.getBenefitsContent(this.site_id, benefits);
    }

    /**
     * 获取会员礼包内容
     * @param gifts
     * @return array|null
     */
    async getMemberGiftsContent(gifts)
    {
        return this.coreMemberService.getGiftContent(this.site_id, gifts);
    }
}
  }

  /**
   * getMemberBenefitsContent
   * 对应 PHP: MemberService_admin::getMemberBenefitsContent()
   * 逻辑类型: undefined - undefined
   */
  async getMemberBenefitsContent(benefits: any[]) {
    // 基于PHP真实逻辑: getMemberBenefitsContent
    // PHP原文: return (new CoreMemberService())->getBenefitsContent($this->site_id, $benefits);     }      /**      * 获取会员礼包内容      * @param array $gifts      * @ret...
return this.coreMemberService.getBenefitsContent(this.site_id, benefits);
    }

    /**
     * 获取会员礼包内容
     * @param gifts
     * @return array|null
     */
    async getMemberGiftsContent(gifts)
    {
        return this.coreMemberService.getGiftContent(this.site_id, gifts);
    }
}
  }

  /**
   * getMemberGiftsContent
   * 对应 PHP: MemberService_admin::getMemberGiftsContent()
   * 逻辑类型: undefined - undefined
   */
  async getMemberGiftsContent(gifts: any[]) {
    // 基于PHP真实逻辑: getMemberGiftsContent
    // PHP原文: return (new CoreMemberService())->getGiftContent($this->site_id, $gifts);     } }...
return this.coreMemberService.getGiftContent(this.site_id, gifts);
    }
}
  }
}
